package com.kun.video.config;

import com.kun.video.AbstractApplication;
import com.kun.video.exception.BizException;
import com.kun.video.ui.dialog.ExceptionDialog;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


/**
 * 全局异常处理
 *
 * @author gzc
 * @since 2025/3/15
 */
public class GlobalExceptionHandler {
    private final static Logger log = LoggerFactory.getLogger(GlobalExceptionHandler.class);

    /**
     * 异常处理，捕获主线程的异常
     */
    public static void handler() {
        // 捕捉未处理的异常
        Thread.setDefaultUncaughtExceptionHandler((t, e) -> {
            // 抛出栈信息
            if (e instanceof BizException biz) {
                log.error(String.format("%s:%s", Thread.currentThread().getStackTrace()[1].getMethodName(), biz.getErrMsg()));
            } else {
                log.error("发生未知异常->", e);
            }
            // 弹窗
            new ExceptionDialog(e).showAndWaitResult();
        });
    }


}
